<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/14
 * Time: 9:36
 */
/**
 * 检测用户是否登录
 * @return integer 0-未登录，大于0-当前登录用户ID
 */
function is_login()
{
    $uid = \think\Session::get('id');
    return is_null($uid) ? 0 : $uid;
}

/**
 * 检测当前用户是否为管理员
 * @return boolean true-管理员，false-非管理员
 */
if (!function_exists('is_administrator')) {
    function is_administrator($uid = null)
    {
        $uid = is_null($uid) ? is_login() : $uid;
        $admin_uids = explode(',', config('USER_ADMINISTRATOR'));//调整验证机制，支持多管理员，用,分隔
        return $uid && (in_array(intval($uid), $admin_uids));//调整验证机制，支持多管理员，用,分隔
    }
}

/**
 * 获取当前管理员id
 * @return array
 */
if (!function_exists('get_administrator')) {
    function get_administrator()
    {
        $admin_uids = explode(',', config('USER_ADMINISTRATOR')); //调整验证机制，支持多管理员，用,分隔
        return $admin_uids;
    }
}


/**
 * t函数用于过滤标签，输出没有html的干净的文本
 * @param string text 文本内容
 * @return string 处理后内容
 */
if (!function_exists('op_t')) {
    function op_t($text, $addslanshes = false)
    {
        $text = nl2br($text);
        $text = real_strip_tags($text);
        if ($addslanshes)
            $text = addslashes($text);
        $text = trim($text);
        return $text;
    }
}


/**过滤函数，别名函数，op_t的别名
 * @param $text
 * @return string
 */
if (!function_exists('real_strip_tags')) {
    function real_strip_tags($str, $allowable_tags = "")
    {
        // $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
        return strip_tags($str, $allowable_tags);
    }
}


if (!function_exists('text')) {
    function text($text, $addslanshes = false)
    {
        return op_t($text, $addslanshes);
    }
}


/**过滤函数，别名函数，op_h的别名
 * @param $text
 * @return string
 */
if (!function_exists('html')) {
    function html($text)
    {
        return op_h($text);
    }
}


/**
 * h函数用于过滤不安全的html标签，输出安全的html
 * @param string $text 待过滤的字符串
 * @param string $type 保留的标签格式
 * @return string 处理后内容
 */
if (!function_exists('op_h')) {
    function op_h($text, $type = 'html')
    {
        // 无标签格式
        $text_tags = '';
        //只保留链接
        $link_tags = '<a>';
        //只保留图片
        $image_tags = '<img>';
        //只存在字体样式
        $font_tags = '<i><b><u><s><em><strong><font><big><small><sup><sub><bdo><h1><h2><h3><h4><h5><h6>';
        //标题摘要基本格式
        $base_tags = $font_tags . '<p><br><hr><a><img><map><area><pre><code><q><blockquote><acronym><cite><ins><del><center><strike>';
        //兼容Form格式
        $form_tags = $base_tags . '<form><input><textarea><button><select><optgroup><option><label><fieldset><legend>';
        //内容等允许HTML的格式
        $html_tags = $base_tags . '<ul><ol><li><dl><dd><dt><table><caption><td><th><tr><thead><tbody><tfoot><col><colgroup><div><span><object><embed><param>';
        //专题等全HTML格式
        $all_tags = $form_tags . $html_tags . '<!DOCTYPE><meta><html><head><title><body><base><basefont><script><noscript><applet><object><param><style><frame><frameset><noframes><iframe>';
        //过滤标签
        $text = real_strip_tags($text, ${$type . '_tags'});
        // 过滤攻击代码
        if ($type != 'all') {
            // 过滤危险的属性，如：过滤on事件lang js
            while (preg_match('/(<[^><]+)(ondblclick|onclick|onload|onerror|unload|onmouseover|onmouseup|onmouseout|onmousedown|onkeydown|onkeypress|onkeyup|onblur|onchange|onfocus|action|background[^-]|codebase|dynsrc|lowsrc)([^><]*)/i', $text, $mat)) {
                $text = str_ireplace($mat[0], $mat[1] . $mat[3], $text);
            }
            while (preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i', $text, $mat)) {
                $text = str_ireplace($mat[0], $mat[1] . $mat[3], $text);
            }
        }
        return $text;
    }
}


/**
 * 时间戳格式化
 * @param int $time
 * @return string 完整的时间显示
 */
if (!function_exists('time_format')) {
    function time_format($time = NULL, $format = 'Y-m-d H:i')
    {
        $time = $time === NULL ? time() : intval($time);
        return date($format, $time);
    }
}


/**
 * 字符串截取，支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param int|string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param bool|string $suffix 截断显示字符
 * @return string
 */
if (!function_exists('msubstr')) {
    function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
    {
        if (function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length, $charset);
        elseif (function_exists('iconv_substr')) {
            $slice = iconv_substr($str, $start, $length, $charset);
            if (false === $slice) {
                $slice = '';
            }
        } else {
            $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("", array_slice($match[0], $start, $length));
        }
        return $suffix ? $slice . '...' : $slice;
    }
}

/**
 *拼接生成完整qiniu 图片路径，获取压缩裁剪图片
 * @param $url
 * @return string
 */
if (!function_exists('qiniuUrl')) {
    function qiniuUrl($url)
    {
        if($url){
            return 'http://' . config('qiniu.host')['image_host'] . '/' . $url . '?imageView2/1/w/85/h/100';
        }else{
            return '/static/images/admin/avatar.png';
        }

    }
}

/**
 * 递归遍历获取menu
 * @param $data
 * @param $pId
 * @return array|string
 */
if (!function_exists('getTree')) {
    function getTree($data, $pId)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pId) {
                $v['pid'] = getTree($data, $v['id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }
}

if (!function_exists('isHidden')) {
    function TextChangeShow($value)
    {
        if (!isset($value) || $value == '' || $value == 0) {
            return 'hidden';
        }
        return '';
    }
}

if (!function_exists('TextChangeHidden')) {

    function TextChangeHidden($value)
    {
        if (!isset($value) || $value == '' || $value == 0) {
            return '';
        }
        return 'hidden';
    }
}

if (!function_exists('accessRule')) {

    function accessRule($node, $rules)
    {
        if (!empty($node)) {
            foreach ($node as $key => $value) {
                if (in_array($value['id'], $rules)) {
                    $node[$key]['isaccess'] = 1;
                } else {
                    $node[$key]['isaccess'] = 0;
                }
                if (empty($value['pid'])) {
                    continue;
                } else {
                    accessRule($node[$key]['pid'], $rules);
                }
            }
            return $node;
        }

    }

    if (!function_exists('writeLog')) {
        function writeLog($info = null)
        {
            $log = new \app\admin\controller\Log();
            return $log->Log($info);

        }
    }

    if (!function_exists('checkAuth')) {
        function checkAuth($name = null)
        {
            $auth=new \app\admin\controller\Auth();
            return $auth->check($name);

        }
    }


    if (!function_exists('send')) {
        function send($status = 0, $message = null, $url = null)
        {
            $base=new \app\admin\controller\Base();
            return $base->send($status, $message,$url);

        }
    }
}